package com.itheima.hello.TCP通信.群聊端口转发;

import javax.xml.crypto.Data;
import java.io.*;
import java.net.Socket;
import java.time.LocalDateTime;

public class ClientReaderThread extends Thread{
    private Socket socket;
    public ClientReaderThread(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            while (true){
                try {
                    String s = dis.readUTF();
                    System.out.println(LocalDateTime.now() + ":");
                    System.out.println(socket.getRemoteSocketAddress() + ":" + s);
                    System.out.println("----------------------------");
                    System.out.println("请输入(输入exit即可退出):");
                } catch (Exception e) {
                    System.out.println(LocalDateTime.now() + ":");
                    System.out.println("自己下线了!!" + socket.getRemoteSocketAddress());
                    System.out.println("----------------------------");
                    dis.close();
                    socket.close();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

